package com.sanjiahantang.youjiaclient.module.category.presenters;


import android.content.Context;
import android.view.View;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.base.BaseMvpPresenter;
import com.sanjiahantang.youjiaclient.base.CallBack;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.module.category.beans.CategoryBean;
import com.sanjiahantang.youjiaclient.module.category.models.ICategoryModel;
import com.sanjiahantang.youjiaclient.module.category.models.impl.CategoryModel;
import com.sanjiahantang.youjiaclient.module.category.views.CategoryView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_TOKEN;
import static com.sanjiahantang.youjiaclient.config.StaticValue.RESPONSE_CODE_SUC;

/**
 * Created by zy on 2017/9/7.
 */

public class CategoryPresenter extends BaseMvpPresenter<CategoryView> {
    private ICategoryModel categoryModel;
    private QuickAdapter<CategoryBean.Data> level1Adapter;
    private int currentPosition;
    private CategoryView.Level1Listener level1Listener;

    public CategoryPresenter(){
        categoryModel = new CategoryModel();
        currentPosition = -1;
    }

    public void setLevel1Listener(CategoryView.Level1Listener level1Listener) {
        this.level1Listener = level1Listener;
    }

    public void onLoad(Context context) {
        Map<String,Object> params = new HashMap<>();
        if (ConfigValue.DATA_KEY == null){
            params.put(KEY_TOKEN,"");
        }else {
            params.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        }
        mView.showProgress();
        categoryModel.loadLevel1(context,params,new Level1CallBack());

        if(level1Adapter == null){
            level1Adapter = new QuickAdapter<CategoryBean.Data>(context, R.layout.item_category_level_1) {
                @Override
                protected void convert(final BaseAdapterHelper helper, final CategoryBean.Data item) {
                    helper.setText(R.id.tvOneLevelName,item.getName());
                    if(item.isSelected()){
                        helper.setVisible(R.id.ivRightLine,true);
                    }else {
                        helper.setVisible(R.id.ivRightLine,false);
                    }
                    helper.getView(R.id.tvOneLevelName).setSelected(item.isSelected());
                    helper.getView().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (currentPosition == helper.getPosition()) {
                                return;
                            }
                            if (currentPosition > -1){
                                level1Adapter.getItem(currentPosition).setSelected(false);
                            }
                            level1Adapter.getItem(helper.getPosition()).setSelected(true);
                            currentPosition = helper.getPosition();
                            level1Adapter.notifyDataSetChanged();
                            level1Listener.onLevel1Clicked(item);
                        }
                    });
                }
            };
        }

        level1Listener.initAdapter(level1Adapter);
    }

    private void loadLevel2(Context context, int id) {
        Map<String,String> params = new HashMap<>();
        if(mView != null)mView.showProgress();
        categoryModel.loadLevel2(context,params,new Level2CallBack());
    }

    public void loadCategoryByType(Context context, int level, Object o) {
    }

    private class Level1CallBack implements CallBack<CategoryBean>{

        @Override
        public void onSuccess(CategoryBean response, Context context, Object tag) {
            hideProgress();
            if(response.getCode() == RESPONSE_CODE_SUC){
                List<CategoryBean.Data> list = response.getData();
                list.get(0).setSelected(true);
                currentPosition = 0;
                level1Adapter.clear();
                level1Adapter.addAll(list);
                level1Listener.onLevel1Clicked(response.getData().get(0));
            }
        }

        @Override
        public void onFailure(String message) {
            hideProgress();
        }
    }

    private void hideProgress() {
        if (mView != null)mView.hideProgress();
    }

    private class Level2CallBack implements CallBack<CategoryBean> {

        @Override
        public void onSuccess(CategoryBean response, Context context, Object tag) {
            hideProgress();
            if("200".equals(response.getCode())){
                level1Listener.onLevel1Clicked(response.getData().get(0));
            }
        }

        @Override
        public void onFailure(String message) {
            hideProgress();
        }
    }
}
